<?php
namespace app\admin\controller;

use think\Db;
use app\admin\model\ReggovernmentModel;
class Capitalmgr extends Base{
	/**
	 * [首页]
	 */
	public function index() {
		if($this->request->isPost()){
			$param = input('post.');
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			$RegCountyModel = new ReggovernmentModel();
			$return['rows'] = $RegCountyModel->getListRows($where,$offset,$limit);
			$return['total'] = $RegCountyModel->getCount($where);
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
		}
		return $this->fetch();
	}

	/**
	 * [添加年资金]
	 */
	public function addYear() {
		$id = input('get.row_id');
		$target = DB::name('users_government')->where('id',$id)->find();
		$this->assign([
			'target' => $target,
			'year' => date("Y",time())
		]);
		return $this->fetch();
	}
	/**
	 * [查看年资金]
	 */
	public function seeYear() {
		$id = input('get.row_id');
		$target = DB::name('users_government')->where('id',$id)->find();
		$data = DB::name('capital_mgr')->where([
			'depart_id' => $target['depart_id']
		])->order('year desc')->select();
		$this->assign([
			'data' => $data
		]);
		return $this->fetch();
	}
	/**
	 * [年资金列表]
	 */
	public function YearList() {
		$param = input('post.');
		print_r($param);
	}
	/**
	 * [保存]
	 */
	public function saveYear() {
		$param = input("post.");
		$check = DB::name('capital_mgr')->where([
			'depart_id' => $param['depart_id'],
			'year' => $param['year']
		])->find();
		if(!empty($check)){
			return ['code'=>0,'msg'=>'该年份的资金已经提交'];
		}
		DB::startTrans();
		try{
			DB::name('capital_mgr')->insert($param);
			DB::commit();
			return ['code'=>1,'msg'=>'保存成功'];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>0,'msg'=>'保存失败，'.$e->getMessage()];
		}
	}
}
